<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">

<div id="app">
    <div class="content-header">
        <h1>系统管理
            <small>角色管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">

            <div class="filter-container">
                <el-input placeholder="角色名" style="width: 200px;" class="filter-item"  v-model="pagination.queryString"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建角色</el-button>
            </div>

            <el-table size="small" current-row-key="id" :data="list" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="角色名" align="center"></el-table-column>
                <el-table-column prop="keyword" label="角色关键词" align="center"></el-table-column>
                <el-table-column prop="description" label="角色描述" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleEdit(scope.row.id)">修改</el-button>
                        <el-button type="primary" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
<!--新增角色标签层-->
            <div class="add-form">
                <el-dialog title="新增角色" :visible.sync="dialogFormVisible">
                    <el-tabs v-model="activeName" type="card">
                        <el-tab-pane label="基本信息" name="first">
                            <el-form ref="roleAddForm" :model="formData" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="角色名称" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="角色关键字" prop="keyword">
                                            <el-input v-model="formData.keyword"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="22">
                                        <el-form-item label="角色描述" prop="description">
                                            <el-input type="textarea" v-model="formData.description"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                        </el-tab-pane>
                        <el-tab-pane label="权限信息" name="second">
                            <div class="checkScrol">
                                <table class="datatable">
                                    <thead>
                                    <tr>
                                        <th>选择</th>
                                        <th>权限名称</th>
                                        <th>关键词</th>
                                        <th>权限说明</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr v-for="permission in permissionList">
                                        <td>
                                            <input :id="permission.id" v-model="permissionIds" type="checkbox" :value="permission.id">
                                        </td>
                                        <td><label :for="permission.id">{{permission.name}}</label></td>
                                        <td><label :for="permission.id">{{permission.keyword}}</label></td>
                                        <td><label :for="permission.id">{{permission.description}}</label></td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </el-tab-pane>
                        <el-tab-pane label="菜单信息" name="third">
                            <div class="checkScrol">
                                <table class="datatable">
                                    <thead>
                                    <tr>
                                        <th>选择</th>
                                        <th>角色名称</th>
                                        <th>关键词</th>
                                        <th>角色说明</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr v-for="menu in menuList">
                                        <td>
                                            <input :id="menu.id" v-model="menuIds" type="checkbox" :value="menu.id">
                                        </td>
                                        <td><label :for="menu.id">{{menu.name}}</label></td>
                                        <td><label :for="menu.id">{{menu.keyword}}</label></td>
                                        <td><label :for="menu.id">{{menu.description}}</label></td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </el-tab-pane>
                    </el-tabs>


                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
<!--编辑角色标签层-->
            <div class="add-form">
                <el-dialog title="编辑角色" :visible.sync="dialogFormVisible4Edit">
                    <el-tabs v-model="activeName" type="card">
                        <el-tab-pane label="基本信息" name="first">
                            <el-form ref="roleAddForm" :model="formData" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="角色名称" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="角色关键字" prop="keyword">
                                            <el-input v-model="formData.keyword"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="22">
                                        <el-form-item label="角色描述" prop="description">
                                            <el-input type="textarea" v-model="formData.description"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                        </el-tab-pane>
                        <el-tab-pane label="权限信息" name="second">
                            <div class="checkScrol">
                                <table class="datatable">
                                    <thead>
                                    <tr>
                                        <th>选择</th>
                                        <th>权限名称</th>
                                        <th>关键词</th>
                                        <th>权限说明</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr v-for="permission in permissionList">
                                        <td>
                                            <input :id="permission.id" v-model="permissionIds" type="checkbox" :value="permission.id">
                                        </td>
                                        <td><label :for="permission.id">{{permission.name}}</label></td>
                                        <td><label :for="permission.id">{{permission.keyword}}</label></td>
                                        <td><label :for="permission.id">{{permission.description}}</label></td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </el-tab-pane>
                        <el-tab-pane label="菜单信息" name="third">
                            <div class="checkScrol">
                                <table class="datatable">
                                    <thead>
                                    <tr>
                                        <th>选择</th>
                                        <th>角色名称</th>
                                        <th>关键词</th>
                                        <th>角色说明</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr v-for="menu in menuList">
                                        <td>
                                            <input :id="menu.id" v-model="menuIds" type="checkbox" :value="menu.id">
                                        </td>
                                        <td><label :for="menu.id">{{menu.name}}</label></td>
                                        <td><label :for="menu.id">{{menu.keyword}}</label></td>
                                        <td><label :for="menu.id">{{menu.description}}</label></td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </el-tab-pane>
                    </el-tabs>


                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleUpdate()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            list: [],//分页列表
            formData: [],//标签层角色表单数据
            dialogFormVisible: false,//控制添加角色窗口显示/隐藏
            dialogFormVisible4Edit: false,//控制角色修改窗口显示/隐藏
            dialogFormVisible4PermissionEdit: false,//控制
            permissionList: [],//标签层关联描述列表
            permissionIds: [],//标签层关联描述ID复选框
            menuList:[],//标签层关联菜单列表
            menuIds:[],//标签层关联菜单ID复选框
            activeName:"first"//添加/编辑窗口Tab标签名称
        },
        //vue周期函数
        mounted() {
            this.findPage();
            // axios.get('/role/findAll').then(resp => (
            //
            //     this.list = resp.data.data.list
            // ))
        },
        methods: {
            //编辑(回显数据)
            handleEdit(id) {
                //重置表单数据
                this.resetForm();
                //展示第一个tab标签
                this.activeName = "first";
                //异步查询角色基本信息
                axios.get("/sys_role/"+id).then(res=>{
                    if (!res.data.flag){
                        this.$message.error(res.data.message);
                        return;
                    }
                    this.formData=res.data.data;
                    //发送ajax请求 ,用户点击新增角色时 , 查询所有权限信息
                    var url = "/sys_role/findAllPermission";
                    axios.get(url).then(response => {
                        if (response.data.flag) {
                            this.permissionList = response.data.data;
                            return;
                        }
                        //提示错误信息
                        this.$message.error(response.data.message);
                    });
                    //发送ajax请求 ,用户点击新增角色时 , 查询所有菜单信息
                    var url = "/sys_role/findAllMenu";
                    axios.get(url).then(response => {
                        if (response.data.flag) {
                            this.menuList = response.data.data;
                            return;
                        }
                        //提示错误信息
                        this.$message.error(response.data.message);
                    });
                    //查询角色关联权限信息
                    axios.get("/sys_role/"+id+"/permissions").then(res=>{
                        if (!res.data.flag) {
                            this.$message.error(res.data.message);
                            return;
                        }
                        this.permissionIds=res.data.data;
                    });
                    //查询角色关联菜单信息
                    axios.get("/sys_role/"+id+"/menus").then(res=>{
                        if (!res.data.flag) {
                            this.$message.error(res.data.message);
                            return;
                        }
                        this.menuIds=res.data.data;
                    });
                });
                //显示编辑框
                this.dialogFormVisible4Edit = true;
            },
            //编辑窗口操作
            handleUpdate() {
                //校验权限及菜单勾选
                if(this.permissionIds.length == 0&&this.menuIds.length==0){
                    this.$message.warning("您还没有勾选权限或者菜单信息！");
                    return ;
                }
                //发送异步请求完成更新
                axios.put("/sys_role/updata?permissionIds="+this.permissionIds+"&&menuIds="+this.menuIds,this.formData).then(res=>{
                    //更新成功
                    if (res.data.flag){
                        //隐藏弹出框
                        this.dialogFormVisible4Edit=false;
                        this.$message.success(res.data.message);
                        return;
                    }
                    //失败
                    this.$message.error(res.data.message);
                }).finally(()=>{
                    //刷新页面
                    this.findPage();
                })
            },
            // 重置角色表单
            resetForm() {
                this.formData = {};
            },
            //新增窗口操作
            handleCreate() {
                //重置表单数据
                this.resetForm();
                //默认切换到第一个标签页（基本信息）
                this.activeName = 'first';
                //展示新增弹出框
                this.dialogFormVisible = true;
                //重置用户勾选的权限id数据
                this.permissionIds = [];
                //重置用户勾选的菜单id数据
                this.menuIds = [];
                //发送ajax请求 ,用户点击新增角色时 , 查询所有权限信息及菜单信息
                var url = "/sys_role/findAllPermission";
                axios.get(url).then(response => {
                    if (response.data.flag) {
                        this.permissionList = response.data.data;
                        return;
                    }
                    //提示错误信息
                    this.$message.error(response.data.message);
                });
                //发送ajax请求 ,用户点击新增角色时 , 查询所有菜单信息
                var url = "/sys_role/findAllMenu";
                axios.get(url).then(response => {
                    if (response.data.flag) {
                        this.menuList = response.data.data;
                        return;
                    }
                    //提示错误信息
                    this.$message.error(response.data.message);
                });
            },
            //新增角色
            handleAdd() {
                //校验权限及菜单勾选
                if(this.permissionIds.length == 0&&this.menuIds.length==0){
                    this.$message.warning("您还没有勾选权限或者菜单信息！");
                    return ;
                }
                //发送异步请求
                axios.post("/sys_role/add?permissionIds="+ this.permissionIds+"&&menuIds="+this.menuIds,this.formData).then(res=>{
                    //隐藏新增窗口
                    this.dialogFormVisible = false;
                    //判断返回数据
                    if (res.data.flag){
                        //成功
                        this.$message.success(res.data.message);
                    } else {
                        //失败
                        this.$message.error(res.data.message);
                    }
                }).finally(()=>{
                    //刷新页面
                    this.findPage();
                });
            },
            //删除角色
            handleDelete(id) {
                //提示用户
                this.$confirm("您确定要删除此记录吗?","提示",{
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{
                    //发送异步请求，删除角色
                    axios.delete("/sys_role/"+id).then(res=>{
                        //删除成功
                        if (res.data.flag){
                            this.$message.success(res.data.message);
                            return;
                        }
                        //失败
                        this.$message.error(res.data.message);
                    }).finally(()=>{
                        //刷新页面
                        this.findPage();
                    });
                });
            },
            //分页查询
            findPage() {
                //发送异步请求
                axios.get("/sys_role/findPage",{params:this.pagination}).then(
                    res=>{
                        if (res.data.flag){
                            this.list=res.data.data.rows;
                            this.pagination.total = res.data.data.total;
                            return;
                        }
                        //失败
                        this.$message.error(res.data.message);
                    }
                )
            },
            //切换页
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            }
        }
    })
</script>
</html>